<template>
  <div>
    <el-dialog
      :title="title"
      width="652px"
      :visible.sync="isShow"
      :before-close="handleClose"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form ref="form" label-width="135px" :rules="rules" :model="params">
        <el-form-item label="户名" prop="tenantName">
          <el-input disabled v-model="params.tenantName"></el-input>
        </el-form-item>
        <el-form-item label="开户行" prop="accountName">
          <el-input :disabled="btnText === '返回'" placeholder="请输入开户行" v-model="params.accountName"></el-input>
        </el-form-item>
        <el-form-item label="签约企业对公账户" prop="accountNumber">
          <el-input
            :disabled="btnText === '返回'"
            placeholder="请输入签约企业对公账户"
            v-model="params.accountNumber"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="submit()">{{ btnText }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { isInteger } from '@/utils/validator.js'
export default {
  name: 'OfflinePop',
  data() {
    const checkAccountNumber = (rule, value, callback) => {
      if (!isInteger(value)) {
        callback(new Error('只能输入数字'))
      } else {
        callback()
      }
    }

    return {
      isShow: false,
      title: '',
      btnText: '',
      rules: {
        accountName: [{ required: true, message: '请输入开户行' }],
        accountNumber: [{ required: true, message: '请输入签约企业对公账户' }, { validator: checkAccountNumber }],
      },
      params: {
        applyId: '',
        tenantName: '',
        accountName: '',
        accountNumber: '',
      },
      isRequestDetailInfo: false,
    }
  },
  methods: {
    async show(item) {
      console.log(1111, item)
      this.isRequestDetailInfo = item.isRequestDetailInfo
      this.isShow = true
      this.title = item.title
      this.btnText = item.btnText
      this.params.applyId = item.id
      if (this.btnText === '返回' && this.isRequestDetailInfo) {
        await this.getInfoRequest()
        return
      }
      this.params.tenantName = item.tenantName
      this.params.accountName = item.accountName
      this.params.accountNumber = item.accountNumber
    },
    submit() {
      if (this.btnText === '返回') return this.handleClose()
      this.$refs['form'].validate(async valid => {
        if (!valid) return

        await this.sendSubmitRequest()
        this.resetForm()
        this.$emit('closePop', true)
      })
    },
    handleClose() {
      this.resetForm()
      this.$emit('closePop')
    },
    resetForm() {
      this.isShow = false
      this.params.applyId = ''
      this.params.tenantName = ''
      this.params.accountName = ''
      this.params.accountNumber = ''
      this.isRequestDetailInfo = false
      this.$refs['form'].resetFields()
    },
    async sendSubmitRequest() {
      await this.$http.post(`/credit_applys/${this.params.applyId}/sign`, this.params)
      this.$message.success('操作成功')
    },
    async getInfoRequest() {
      const result = await this.$http.get(`/credit_applys/${this.params.applyId}/sign_info`)
      this.params.tenantName = result.uscName
      this.params.accountName = result.accountName
      this.params.accountNumber = result.accountNumber
    },
  },
}
</script>

<style lang="scss" scoped>
.btn {
  width: 400px;
}
::v-deep {
  .el-input__inner {
    width: 400px;
  }
}
</style>
